package models;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

import play.db.jpa.Model;



@Entity
public class UserPlace  extends Model{
      
    private long userCityId;
    private long placeInfoId;
    private String placeName;
	public UserPlace(String name) {
       
		this.placeName = name;
		// TODO Auto-generated constructor stub
	}
	

	public  static UserPlace findOrCreatePlace(double latitude , double longitude, String name){
		 Cordinate cordinate = Cordinate.findOrCreate(latitude, longitude);
	     if(cordinate.getId() == null){
	    	 return new UserPlace(name);
	     }
	     PlaceInfo placeInfo = PlaceInfo.find("cordinateId = ?", cordinate.getId()).first();
	     UserPlace userPlace = UserPlace.find("placeInfoId", placeInfo.getId()).first();   
	     if(userPlace == null){
	        return new UserPlace(name);
	     }
	     return userPlace;
	}
	
	public UserPlace saveUserPlace(UserPlace addedPlace ,PlaceInfo placeInfo , Cordinate cordinate , UserPlaceType placeType){
		UserPlaceType userPlaceType = this.addPlaceType(placeType.getPlaceType(), placeType.getPlaceTypeDesc());  
	     cordinate.save();
	     placeInfo.setCordinateId(cordinate.getId());
	     placeInfo.save();
	     cordinate.setPlaceInfoId(placeInfo.getId());
	     cordinate.save();
	     addedPlace.setPlaceInfoId(placeInfo.getId());
	     addedPlace.save();
	     UserPlaceTypeMapping mapping = new UserPlaceTypeMapping(addedPlace.getId(), userPlaceType.getId());
	     mapping.save();
	     return addedPlace;
	}
	
	public UserPlace saveUserPlace(UserPlace addedPlace , UserPlaceType placeType){
		if(addedPlace.getPlaceInfoId() > 0){
			UserPlaceType userPlaceType = this.addPlaceType(placeType.getPlaceType(), placeType.getPlaceTypeDesc()); 
			addedPlace.save();
			UserPlaceTypeMapping mapping = new UserPlaceTypeMapping(addedPlace.getId(), userPlaceType.getId());
			mapping.save();
		}
		return addedPlace;
	}
	
	
	public UserPlaceType  addPlaceType(int placeTypeVal,String placeTypeDesc){
		UserPlaceType userPlaceType = UserPlaceType.findOrCreatePlaceType(placeTypeVal, placeTypeDesc);
		   if(userPlaceType.getId() == null){
			  userPlaceType.save();
		   }
		   return userPlaceType;
	}
	
	
	public Comment addComment(Comment comment){
		Comment savedComment = null; 
		if(this.getId() != null){
		   comment.setUserPlaceId(this.getPlaceInfoId());
		   savedComment = comment.save();
		 }
		return savedComment;
	 }

	public PlaceRanking addRanking(PlaceRanking ranking){
		PlaceRanking savedRanking  = null;
		if(this.getId() != null){
		   ranking.setUserPlaceId(this.getId());
		   savedRanking =  ranking.save();
		}
		return savedRanking;
	}
	public UserPlace(long userCityId,long placeInfoId){
		this.placeInfoId=placeInfoId;
		this.userCityId=userCityId;
	}
	public UserCity getCity() {
	  UserCity userCity = UserCity.findById(this.getUserCityId());
	  return userCity;
	}
	public void setCity(UserCity userCity) {
		this.userCityId = userCity.getId();
	}


	public List<UserPlaceType> getUserPlaceTypes() {
		return null;
	}


	


	public List<Comment> getComments() {
		List<Comment> comments = null;
		comments = Comment.find("userPlaceId = ?", this.getId()).fetch();
		return comments;
	}

   public List<PlaceRanking> getRankings(){
	   List<PlaceRanking> rankings = PlaceRanking.find("userPlaceId = ?", this.getId()).fetch();
	   return rankings;
   }
	


	public PlaceInfo getPlaceInfo() {
		PlaceInfo placeInfo = null;
		placeInfo = PlaceInfo.findById(this.getPlaceInfoId());
		return placeInfo;
	}


	public void setPlaceInfo(PlaceInfo placeInfo) {
		this.placeInfoId = placeInfo.getId();
	}


	public long getUserCityId() {
		return userCityId;
	}


	public void setUserCityId(long userCityId) {
		this.userCityId = userCityId;
	}


	public long getPlaceInfoId() {
		return placeInfoId;
	}


	public void setPlaceInfoId(long placeInfoId) {
		this.placeInfoId = placeInfoId;
	}


	public String getPlaceName() {
		return placeName;
	}


	public void setPlaceName(String placeName) {
		this.placeName = placeName;
	}
    
}
